//
//  MyGetControllerTool.swift
//  Intersection
//
//  Created by 好东家 on 2022/8/11.
//

import UIKit
//
/////获取控制器工具类
//class MyGetControllerTool: NSObject {

// MARK:  获取当前Window
@MainActor
public var getWindow: UIWindow {
    return UIApplication.shared.currentWindow ?? UIWindow()
}


//MARK:  获取当前WindowRootVC
///获取当前WindowRootVC
@MainActor
public var getWindowRootVC: UIViewController{
    if #available(iOS 13.0, *) {
        return UIApplication.shared.currentWindow?.rootViewController ?? UIViewController()
    }
    
    let window = UIApplication.shared.delegate?.window
    return (window??.rootViewController)!
}

//MARK:  获取当前显示的ViewController
///返回当前控制器
@MainActor
public var getCurrentVC: UIViewController{
    
    var currenVC = getWindowRootVC
    
    while true {
        
        if let presentedVC = currenVC.presentedViewController {
            
            currenVC = presentedVC
            
        }else if let visibleVC = (currenVC as? UINavigationController)?.visibleViewController {
            
            currenVC = visibleVC
            
        }else if let selectedVC = (currenVC as? UITabBarController)?.selectedViewController {
            
            currenVC = selectedVC
            
        }else if let children = currenVC.children.last {
                
            currenVC = children
            
        }else{
            
            return currenVC
            
        }
        
    }
    
    return currenVC
}

// MARK: 获取当前Navigation
///返回当前Navigation
@MainActor
public var getNavigationVC: UINavigationController?{
    
    var vc = getWindowRootVC
    
    if let nav = vc as? UINavigationController {
        // 如果是导航控制器，返回它
        return nav
    } else if let tab = vc as? UITabBarController {
        // 如果是标签栏控制器，递归获取选中控制器的导航控制器
        return getCurrentNavigationController(from: tab.selectedViewController)
    } else if let presented = vc.presentedViewController {
        // 如果有模态控制器，递归获取模态控制器的导航控制器
        return getCurrentNavigationController(from: presented)
    } else {
        // 其他情况，尝试获取子控制器的导航控制器
        return vc.navigationController
    }
    
}

///返回当前Navigation
@MainActor
public func getCurrentNavigationController(from vc: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UINavigationController? {
    guard let vc = vc else { return nil }

    if let nav = vc as? UINavigationController {
        // 如果是导航控制器，返回它
        return nav
    } else if let tab = vc as? UITabBarController {
        // 如果是标签栏控制器，递归获取选中控制器的导航控制器
        return getCurrentNavigationController(from: tab.selectedViewController)
    } else if let presented = vc.presentedViewController {
        // 如果有模态控制器，递归获取模态控制器的导航控制器
        return getCurrentNavigationController(from: presented)
    } else {
        // 其他情况，尝试获取子控制器的导航控制器
        return vc.navigationController
    }
}


// MARK: 获取当前TabBarController
///返回当前TabBarController
@MainActor
func getCurrentTabBarController(from vc: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UITabBarController? {
    guard let vc = vc else { return nil }

    if let tabBar = vc as? UITabBarController {
        // 已经是 TabBarController，直接返回
        return tabBar
    } else if let nav = vc as? UINavigationController {
        // 是导航控制器，递归查找栈顶控制器的 TabBarController
        return getCurrentTabBarController(from: nav.topViewController)
    } else if let presented = vc.presentedViewController {
        // 有模态弹出控制器，递归查找
        return getCurrentTabBarController(from: presented)
    } else {
        // 其他情况，尝试返回当前控制器的 tabBarController 属性
        return vc.tabBarController
    }
}




// MARK: 获取父控制器
/// 获取父控制器
/// 从 currentView 开始，向上遍历其父视图，找到第一个对应类型 T 的视图控制器
@MainActor
public func getSuperVC<T: UIViewController>(currentView: UIView, viewController: T.Type) -> T? {
    var view = currentView.superview
    
    while let currentView = view {
        if let nextResponder = currentView.next, nextResponder.isKind(of: T.self) {
            return nextResponder as? T
        }
        view = currentView.superview
    }
    
    return nil
}


//}
